# -*- coding: utf-8 -*-
"""A Namespace describes a QPBuild Namespace. These entities are created by
polling the namespace server(s) and merging the results into the database.
"""

from sqlalchemy import *
from sqlalchemy.orm import mapper, relation
from sqlalchemy import Table, ForeignKey, Column
from sqlalchemy.types import Integer, Unicode

from abserver.model import DeclarativeBase, metadata, DBSession


class NotificationMethod(DeclarativeBase):
    # table options
    __tablename__ = 'notificationmethod'

    # columns
    notification_id = Column(Integer, primary_key=True, autoincrement=True)
    notification_name = Column(Unicode(64), nullable=False, unique=True)
    notification_description = Column(Unicode(), nullable=True)
    notification_supported = Column(Boolean)

    NM_ETEXT, NM_EHTML, NM_RSS, NM_IM = range(4)

    NOTIFICATION_METHOD = [
                           (NM_ETEXT, "Text EMail"),
                           (NM_EHTML, "HTML EMail"),
                           (NM_RSS, "RSS Feed"),
                           (NM_IM, "Instant Message"),
                          ]

    # Get notification by name
    @classmethod
    def get_notificationmethod(cls, name):
        method = DBSession.query(cls).filter(cls.notification_name == name)
        return method

    # Find all supported methods
    @classmethod
    def get_supported_notifications(cls):
        supported_methods = cls.select(and_(cls.notification_supported == 1))
        return supported_methods


class BuildNotification(DeclarativeBase):
    # table options
    __tablename__ = 'buildnotification'

    # columns
    notification_type = Column(Integer, primary_key=True, index=True, autoincrement=True)

    user_id = Column(Integer, ForeignKey('tg_user.user_id'))
    component_id = Column(Integer, ForeignKey('component.component_id'))
    branch_id = Column(Integer, ForeignKey('branch.branch_id'))
    notificationmethod_id = Column(Integer, ForeignKey('notificationmethod.notification_id'))

    # relationships
    user = relation('User', backref='buildnotifications')
    component = relation('Component', backref='buildnotifications')
    branch = relation('Branch', backref='buildnotifications')
    method = relation('NotificationMethod', backref='buildnotifications')

    BN_BUILDSUCCESS, BN_BUILDFAILURE, BN_TESTSUCCESS, BN_TESTFAILURE = range(4)

    BUILD_NOTIFICATION_TYPE = [
                               (BN_BUILDSUCCESS, "Notify on Build Success"),
                               (BN_BUILDFAILURE, "Notify on Build Failure"),
                               (BN_TESTSUCCESS, "Notify on Test Success"),
                               (BN_TESTFAILURE, "Notify on Test Failure"),
                              ]

    def __init__(self, component, user, branch, notification, notification_type):
        self.component = component
        self.user = user
        self.branch = branch
        self.notification = notification
        self.notification_type = notification_type

    # Find all notifications to be sent
    @classmethod
    def find_notifications(cls, component, branch, notification_type):
        notifications = cls.query.filter(and_(cls.component == component,
                                              cls.branch == branch,
                                              cls.notification_type == notification_type)).all()
        return notifications

    # Find all notifications by user for a specific build
    @classmethod
    def find_notifications_by_user(cls, user, branch, component):
        notifications = cls.query.filter(and_(cls.component == component,
                                              cls.branch == branch.branch,
                                              cls.user == user)).all()
        return notifications
